package com.mstar.qs.common.bean;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import com.morningstar.qs.api.basic.util.DatetimeUtil;
import com.morningstar.qs.api.basic.util.DatetimeUtil.DatetimeMetadata;

public class CommonExchange implements Serializable, Comparator<CommonExchange> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5332405762272758230L;

	private final Map<Integer, CommonMarket> markets = new HashMap<Integer, CommonMarket>();

	protected int exchId;
	protected String shortName;
	protected String timezoneId;
	protected String name;
	protected String micCode;
	protected String regionName;
	protected int delayMinute;
	protected String address;
	protected String website;
	protected String description;

	private int gmtOffsetByMinutes;
	private int estOffsetByMinutes;

	public CommonExchange() {
	}

	public CommonExchange(int exchId) {
		this.exchId = exchId;
	}

	public boolean duringWeekend() {
		int dayOfWeek = obtainMarketDayOfWeek();
		if (dayOfWeek == 1 || dayOfWeek == 7)
			return true;
		return false;
	}

	public boolean duringWeekend(DatetimeMetadata dm) {
		int dayOfWeek = dm.getDayOfWeek();
		if (dayOfWeek == 1 || dayOfWeek == 7)
			return true;
		return false;
	}

	public int obtainMarketDayOfWeek() {
		if (timezoneId == null)
			return -1;
		else {
			Calendar c = DatetimeUtil.getCalendar(timezoneId);
			return c.get(Calendar.DAY_OF_WEEK);
		}
	}

	public DatetimeMetadata datetimeMetadata() {
		return DatetimeUtil.datetimeMetadata(timezoneId);
	}
	public DatetimeMetadata datetimeGMTMetadata() {
		Calendar c=DatetimeUtil.getCalendar(timezoneId);
		Calendar gmtc=DatetimeUtil.plusTime(c.getTime(), Calendar.MINUTE, -(gmtOffsetByMinutes));
		return DatetimeUtil.datetimeMetadata(gmtc);
	}

	public boolean equals(Object obj) {
		if (!(obj instanceof CommonExchange))
			return false;
		CommonExchange ex = (CommonExchange) obj;

		return exchId == ex.getExchId();
	}

	public Map<Integer, CommonMarket> getMarkets() {
		return markets;
	}

	@Override
	public int hashCode() {
		return exchId;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("exchId:").append(exchId).append("\t");
		sb.append("shortName:").append(shortName).append("\t");
		sb.append("timezoneId:").append(timezoneId).append("\t");
		sb.append("name:").append(name).append("\t");
		sb.append("micCode:").append(micCode).append("\t");
		sb.append("regionName:").append(regionName).append("\t");
		sb.append("delayMinute:").append(delayMinute).append("\t");
		sb.append("address:").append(address).append("\t");
		sb.append("website:").append(website).append("\t");
		sb.append("description:").append(description).append("\t");
		return sb.toString();
	}

	public int getEstOffsetByMinutes() {
		return estOffsetByMinutes;
	}

	public void setEstOffsetByMinutes(int estOffsetByMinutes) {
		this.estOffsetByMinutes = estOffsetByMinutes;
	}

	public void setGmtOffsetByMinutes(int gmtOffsetByMinutes) {
		this.gmtOffsetByMinutes = gmtOffsetByMinutes;
	}

	public int getExchId() {
		return exchId;
	}

	public void setExchId(int exchId) {
		this.exchId = exchId;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getTimezoneId() {
		return timezoneId;
	}

	public void setTimezoneId(String timezoneId) {
		this.timezoneId = timezoneId;
		if (this.timezoneId != null) {
			gmtOffsetByMinutes = DatetimeUtil.gmtOffset(timezoneId);
			estOffsetByMinutes = DatetimeUtil.estOffset(timezoneId);
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMicCode() {
		return micCode;
	}

	public void setMicCode(String micCode) {
		this.micCode = micCode;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/*
	 * public String getRegionName() { return regionName; }
	 * 
	 * public void setRegionName(String regionName) { this.regionName =
	 * regionName; }
	 */

	public int getDelayMinute() {
		return delayMinute;
	}

	public void setDelayMinute(int delayMinute) {
		this.delayMinute = delayMinute;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public void addMarkets(CommonMarket cm) {
		markets.put(cm.getSecuType(), cm);
	}

	public CommonMarket obtainMarket(int secuType) {
		return markets.get(secuType);
	}
	
	public int minutesOfDay() {
		return DatetimeUtil.minutesOfDay(timezoneId);
	}
	

	@Override
	public int compare(CommonExchange o1, CommonExchange o2) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getGmtOffsetByMinutes() {
		return gmtOffsetByMinutes;
	}

}
